package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    //创建加密对象
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    //获取当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername()throws Exception{
        try{
            User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    // 分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) throws Exception {
        PageResult pageResult = userService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roles ){
        try{
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            userService.add(user,roles);
            return  new Result(true ,MessageConstant.ADD_USER_SUCCESS);
        }catch (Exception e){
            //新增套餐失败
            return new Result(false,MessageConstant.ADD_USER_FALL);
        }

    }

    //根据UserId查询User数据
    @RequestMapping("/findByUserId")
    public Result findByUserId(Integer id){
        try{
            com.itheima.pojo.User user = userService.findByuSerId(id);
            return  new Result(true ,MessageConstant.QUERY_USER_SUCCESS,user);
        }catch (Exception e){
            //新增套餐失败
            return new Result(false,MessageConstant.QUERY_USER_FALL);
        }

    }
    //根据UserId查询User数据
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.itheima.pojo.User user,Integer[] roles){
        try{
            userService.edit(user,roles);
            return  new Result(true ,MessageConstant.UPDATE_USER_SUCCESS);
        }catch (Exception e){
            //新增套餐失败
            return new Result(false,MessageConstant.UPDATE_USER_FALL);
        }
    }
    //根据UserId删除User数据
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            userService.delete(id);
            return  new Result(true ,MessageConstant.DELETE_USER_SUCCESS);
        }catch (Exception e){
            //新增套餐失败
            return new Result(false,MessageConstant.DELETE_USER_FALL);
        }
    }
}
